package com.nsoft.thickclient.trackMove.vehicle;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.JPanel;

import com.nsoft.server.service.main.PartyService;
import com.nsoft.server.service.main.VechileService;
import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.beanUtils.ActionDescriptor;
import com.nsoft.thickclient.beanUtils.NButton;
import com.nsoft.thickclient.controller.ScreenController;
import com.nsoft.thickclient.trackMove.intr.party.Party;
import com.nsoft.thickclient.trackMove.intr.vehicle.Vehicle;

public class VehicleViewController extends ScreenController {

	private ActionDescriptor saveAction = new ActionDescriptor("Save",
			"doSave", this);

	private ActionDescriptor newAction = new ActionDescriptor("New", "doNew",
			this);

	private ActionDescriptor cancelAction = new ActionDescriptor("Cancel",
			"doCancel", this);

	private JPanel mainPanel;

	private Vehicle vehicle;

	private boolean editMode = true;

	private VehicleView VehicleView;

	public VehicleView getVehicleView() {
		return VehicleView;
	}

	public void setVehicleView(VehicleView vehicleView) {
		VehicleView = vehicleView;
	}

	public VehicleViewController(boolean editMode) {
		this(new Vehicle());
		this.editMode = editMode;
	}

	public VehicleViewController() {
		this(new Vehicle());
		editMode = false;
	}

	public VehicleViewController(Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getScreenTitle() {
		return "Manage vehicle";
	}

	@Override
	public JPanel getView() {
		return mainPanel;
	}

	@Override
	public void init() {
		VehicleView = new VehicleView(vehicle, editMode);
		mainPanel = new JPanel(new BorderLayout());
		mainPanel.add(VehicleView, BorderLayout.CENTER);
		mainPanel.add(getButtonPanel(), BorderLayout.PAGE_END);
	}

	private JPanel getButtonPanel() {
		JPanel butonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		butonPanel.add(new NButton(saveAction));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(newAction, !editMode));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(cancelAction));
		return butonPanel;
	}

	public void doSave() {
		VehicleView.getModel();
		VechileService service = FacadeFactoryImpl.getVechileService();
		Vehicle party =service.saveVechile((Vehicle)VehicleView.getModel().getValueObject());
		if (!editMode) {
			VehicleView.setWorkingBean(party);
			doNew();
		} else {
			doCancel();

		}
	}

	public void doNew() {
		VehicleView.getModel().setValueObject(new Vehicle());
		// view.
	}

	public void doCancel() {
		getApplicationController().closeScreen(this);
	}
}
